﻿using Acme.BookStore.Goodss;
using Acme.BookStore.GoodTable;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.GoodsProduct
{
   
    [ApiExplorerSettings(GroupName = "v1")]
    public class GoodsProductServices : ApplicationService, IGoodsProductServices
    {
        public IRepository<GoodsTypes> RoomCodeInfoRepository { get; }
        public GoodsProductServices(IRepository<GoodsTypes> RoomCodeInfoRepository)
        {
            this.RoomCodeInfoRepository = RoomCodeInfoRepository;
        }

        /// <summary>
        /// 商品标签添加
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("api/GoodsProductAdd")]
        public async Task<APIResult> GoodsProductAdd(GoodsProductDTO roomCodeInfoeDTO)
        {
            try
            {
                GoodsTypes roomCodeInfo = ObjectMapper.Map<GoodsProductDTO, GoodsTypes>(roomCodeInfoeDTO);
                await RoomCodeInfoRepository.InsertAsync(roomCodeInfo);
                if (roomCodeInfo == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "商品标签添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 商品标签修改
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("api/GoodsProductUpdate")]
        public async Task<APIResult> GoodsProductUpdate(GoodsProductUpdateDTO roomCodeInfoUpdateDTO)
        {
            try
            {
                var roomType = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == roomCodeInfoUpdateDTO.Id);
                if (roomType == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "商品标签未找到"
                    };
                }

                ObjectMapper.Map(roomCodeInfoUpdateDTO, roomType);
                await RoomCodeInfoRepository.UpdateAsync(roomType);

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "商品标签修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 商品标签列表+查询
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("api/GoodsProductShow")]
        public async Task<APIResult<List<GoodsProductDTO>>> GoodsProductShow(string GoodsTypeName)
        {
            try
            {
                var list = await RoomCodeInfoRepository.GetListAsync(x => x.IsDeleted == false);
                if (GoodsTypeName != null)
                {
                    list = list.Where(x => x.GoodsTypeName == GoodsTypeName).ToList();
                }
                return new APIResult<List<GoodsProductDTO>>()
                {
                    Code = CodeEnum.success,
                    Message = "查询成功",
                    Data = ObjectMapper.Map<List<GoodsTypes>, List<GoodsProductDTO>>(list.ToList())
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 商品标签删除
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("api/GoodsProductDelete")]
        public async Task<APIResult> GoodsProductDelete(int Id)
        {
            try
            {
                var roomcode = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == Id);
                if (roomcode == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的商品"
                    };
                }
                await RoomCodeInfoRepository.DeleteAsync(roomcode);
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
